package com.alipay.util;

import javax.servlet.http.HttpServletRequest;

import com.fish.anne.model.Order;
import com.fish.anne.model.OrderPaymentCfg;
import com.fish.anne.webapp.util.RequestUtil;

public class AlipayParameter {

	public static final String aliSyncCallbackUrl = "aliSyncCallback.do";
	public static final String aliAsyncCallbackUrl = "aliAsyncCallback.do";

	// 支付接口
	private String paygateway;

	// /////////////////////不需要更改值的参数 ///////////////////////
	/**
	 * trade_create_by_buyer create_parnter_trader_by_buyer
	 */
	private String service;
	private String sign_type;
	private String input_charset;
	private String payment_type;

	// /////////////////////特殊参数 ///////////////////////
	private String sign;

	// /////////////////////需要设置的参数 ----begin///////////////////////
	private String partner;
	private String key;
	private String seller_email;
	private String out_trade_no;
	private String subject;

	// 双接口，担保交易参数
	private String price;
	private String quantity;
	private String logistics_type;
	private String logistics_fee;
	private String logistics_payment;

	// 立即到帐参数/
	// price、quantity能代替total_fee。即存在total_fee，就不能存在price和quantity；存在price、
	// quantity，就不能存在total_fee。
	private String total_fee;

	// /////////////////////需要设置的参数 -----end///////////////////////

	private String body;
	private String show_url;
	private String discount;
	// 通知接收URL(本地测试时，服务器返回无法测试)
	private String return_url;
	private String notify_url;
	private String ItemUrl;

	public static AlipayParameter initAlipayParameter(HttpServletRequest request, Order order, OrderPaymentCfg orderPaymentCfg) {

		AlipayParameter alipayParameter = new AlipayParameter();

		String paygateway = "https://www.alipay.com/cooperate/gateway.do?";
		String service = orderPaymentCfg.getAlipayType();

		String sign_type = "MD5";

		String out_trade_no = order.getOrderId().toString();
		String input_charset = "utf-8";

		String partner = orderPaymentCfg.getPayMemberId(); 
		String key = orderPaymentCfg.getPayPrivateKey();

		String body = out_trade_no;

		String total_fee = order.getTotalAmount().toString();

		String price = total_fee;
		String quantity = "1";

		String payment_type = "1";
		String seller_email = orderPaymentCfg.getSellerEmail();

		String subject = out_trade_no;

		

		String path = RequestUtil.getAppURL(request);// "http://190.10.2.33:7001/";
		// 支付完成后跳转返回的网址URL
		String return_url = path + "/" + aliSyncCallbackUrl;
		// 通知接收URL(本地测试时，服务器返回无法测试)
		String notify_url = path + "/" + aliAsyncCallbackUrl;
		String sign = "";

		if (service.equals("create_direct_pay_by_user")) {
			sign = Payment.CreateDirectUrl(service, sign_type, out_trade_no, input_charset, partner, key,  body, price,quantity, payment_type,
					seller_email, subject, notify_url, return_url);
		} else {

			String logistics_fee = "EMS";
			String logistics_type = "0";
			String logistics_payment = "SELLER_PAY";
			alipayParameter.setLogistics_fee(logistics_fee);
			alipayParameter.setLogistics_type(logistics_type);
			alipayParameter.setLogistics_payment(logistics_payment);
			sign = Payment.CreateUrl(service, partner, seller_email, return_url, notify_url,   out_trade_no, subject, price, logistics_fee,
					logistics_type, logistics_payment, quantity, input_charset, key, sign_type,body);
		}

		alipayParameter.setPaygateway(paygateway);

		alipayParameter.setService(service);
		alipayParameter.setSign_type(sign_type);
		alipayParameter.setInput_charset(input_charset);
		alipayParameter.setPayment_type(payment_type);

		alipayParameter.setPartner(partner);
		alipayParameter.setKey(key);
		alipayParameter.setSeller_email(seller_email);
		alipayParameter.setOut_trade_no(out_trade_no);
		alipayParameter.setSubject(subject);
		alipayParameter.setSign(sign);

		alipayParameter.setTotal_fee(total_fee);
		alipayParameter.setPrice(price);
		alipayParameter.setQuantity(quantity);

		alipayParameter.setBody(body);
		
		// not use
		String show_url = "http://www.hubaobei.com";
		alipayParameter.setShow_url(show_url);

		alipayParameter.setReturn_url(return_url);
		alipayParameter.setNotify_url(notify_url);

		return alipayParameter;
	}

	public String getPaygateway() {
		return paygateway;
	}

	public void setPaygateway(String paygateway) {
		this.paygateway = paygateway;
	}

	public String getService() {
		return service;
	}

	public void setService(String service) {
		this.service = service;
	}

	public String getSign_type() {
		return sign_type;
	}

	public void setSign_type(String signType) {
		sign_type = signType;
	}

	public String getInput_charset() {
		return input_charset;
	}

	public void setInput_charset(String inputCharset) {
		input_charset = inputCharset;
	}

	public String getPayment_type() {
		return payment_type;
	}

	public void setPayment_type(String paymentType) {
		payment_type = paymentType;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getPartner() {
		return partner;
	}

	public void setPartner(String partner) {
		this.partner = partner;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getSeller_email() {
		return seller_email;
	}

	public void setSeller_email(String sellerEmail) {
		seller_email = sellerEmail;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public void setOut_trade_no(String outTradeNo) {
		out_trade_no = outTradeNo;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getQuantity() {
		return quantity;
	}

	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}

	public String getLogistics_type() {
		return logistics_type;
	}

	public void setLogistics_type(String logisticsType) {
		logistics_type = logisticsType;
	}

	public String getLogistics_fee() {
		return logistics_fee;
	}

	public void setLogistics_fee(String logisticsFee) {
		logistics_fee = logisticsFee;
	}

	public String getLogistics_payment() {
		return logistics_payment;
	}

	public void setLogistics_payment(String logisticsPayment) {
		logistics_payment = logisticsPayment;
	}

	public String getTotal_fee() {
		return total_fee;
	}

	public void setTotal_fee(String totalFee) {
		total_fee = totalFee;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getShow_url() {
		return show_url;
	}

	public void setShow_url(String showUrl) {
		show_url = showUrl;
	}

	public String getDiscount() {
		return discount;
	}

	public void setDiscount(String discount) {
		this.discount = discount;
	}

	public String getReturn_url() {
		return return_url;
	}

	public void setReturn_url(String returnUrl) {
		return_url = returnUrl;
	}

	public String getNotify_url() {
		return notify_url;
	}

	public void setNotify_url(String notifyUrl) {
		notify_url = notifyUrl;
	}

	public String getItemUrl() {
		return ItemUrl;
	}

	public void setItemUrl(String itemUrl) {
		ItemUrl = itemUrl;
	}

}
